layui.define(['table', 'form', 'admin', 'upload'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;

    //
    table.render({
        elem: '#LAY-administrator-manage'
        , url: '/admin/admin/getList'
        , cols: [[
            { field: 'id', width: 100, title: 'ID', sort: true }
            , { field: 'name', width: 200, title: '后台显示名称' }
            , { field: 'account', minWidth: 200, title: '后台登录账号名称' }
            , {
                field: '', width: 200, title: '角色', templet: function (d) {
                    return d.role ? d.role.name : '';
                }
            }
            , {
                field: '', width: 100, title: '状态', templet: function (d) {
                    return d.status == 1 ? '<span class="layui-badge layui-bg-green">正常</span>' : '<span class="layui-badge layui-bg-orange">封禁</span>';
                }
            }
            , { title: '操作', width: 550, align: 'center', fixed: 'right', toolbar: '#table-administratorlist-administrator' }
        ]]
        , page: false
        , limit: 0
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-administrator-manage)', function (obj) {
        if (obj.event === 'del') {
            layer.confirm('确认删除该账号？', function (index) {
                admin.req({
                    url: '/admin/admin/del_post'
                    , data: { "id": obj.data.id }
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-administrator-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    }
                });
            });
        } else if (obj.event === 'edit') {
            var index = layer.open({
                type: 2
                , title: '编辑角色'
                , content: '/admin/admin/edit?id=' + obj.data.id
                , maxmin: true
            });
            layer.full(index);
        } else if (obj.event === 'reset') {
            layer.confirm('确认重置该账号的密码？', function (index) {
                admin.req({
                    url: '/admin/admin/reset_password'
                    , data: { "id": obj.data.id }
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-administrator-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    }
                });
            });
        } else if (obj.event === 'reset_google_secret') {
            layer.confirm('确认重置该账号的gogle身份验证吗？', function (index) {
                admin.req({
                    url: '/admin/admin/reset_google_secret'
                    , data: { "id": obj.data.id }
                    , success(res) {
                        if (res.code == 0) {
                            table.reload("LAY-administrator-manage");
                            layer.msg(res.msg, { icon: 1 });
                        } else {
                            layer.msg(res.msg, { icon: 5 });
                        }
                    }
                });
            });
        }

        if (obj.event === 'sub_anchors') {
            var index = layer.open({
                type: 2
                , title: '设定主播会话'
                , content: '/admin/admin/sub_anchors?id=' + obj.data.id
                , max: true
            });
            layer.full(index);
        }

    });

    table.render({
        elem: "#LAY-user-manage",
        url: "/admin/user/anchor_list",
        initSort: {
            field: "auth_time",
            type: "desc",
        },
        sort: true,
        cols: [
            [
                { field: "id", width: 100, title: "ID", fixed: "left" },
                {
                    field: "nick_name",
                    title: "昵称",
                    minWidth: 100,
                    templet: function (d) {
                        return decodeURI(d.nick_name);
                    },
                },
                { field: "account", title: "账号", width: 150 },
                {
                    field: "",
                    title: "主播姓名",
                    width: 100,
                    templet: function (d) {
                        if (d.anchor_auth) {
                            return d.anchor_auth.real_name;
                        } else {
                            return "";
                        }
                    },
                },
                { field: "fans_count", title: "粉丝", width: 100 },
                { field: "tag", title: "标签", width: 100 },
                {
                    field: "",
                    title: "联系QQ",
                    width: 150,
                    templet: function (d) {
                        if (d.anchor_auth) {
                            return d.anchor_auth.qq;
                        } else {
                            return "";
                        }
                    },
                },
                {
                    field: "",
                    title: "联系微信",
                    width: 150,
                    templet: function (d) {
                        if (d.anchor_auth) {
                            return d.anchor_auth.we_chat;
                        } else {
                            return "";
                        }
                    },
                },
                {
                    field: "",
                    title: "直播状态",
                    width: 100,
                    templet: function (d) {
                        if (d.in_live) {
                            return "<span class='layui-badge layui-bg-blue'>直播中</span>";
                        } else {
                            return "<span class='layui-badge layui-bg-cyan'>未开播</span>";
                        }
                    },
                },
                {
                    field: "",
                    title: "直播间公告",
                    width: 150,
                    templet: function (d) {
                        if (d.anchor_live_notice) {
                            return d.anchor_live_notice.content;
                        } else {
                            return "";
                        }
                    },
                },
                {
                    field: "",
                    title: "主播类型",
                    width: 100,
                    templet: function (d) {
                        let type;
                        if (d.anchor_type == 1) type = "普通主播";
                        if (d.anchor_type == 2) type = "公司主播";
                        if (d.anchor_type == 3) type = "测试主播";
                        return type;
                    },
                },

                {
                    field: "",
                    title: "竞猜权限",
                    width: 100,
                    templet: function (d) {
                        if (d.is_guess_host) {
                            return "<span class='layui-badge layui-bg' style='background-color: #1E9FFF;'>已开启</span>";
                        }

                        return "<span class='layui-badge' style='border: 1px solid #e6e6e6;background-color: #FBFBFB;color: #C9C9C9;'>未开启</span>";
                    },
                },

                { field: "anchor_point", width: 100, title: "主播经验值" },

                {
                    field: "auth_time",
                    title: "认证时间",
                    width: 170,
                    sort: true,
                    templet: function (d) {
                        return d.auth_time ?? "";
                    },
                },
                {
                    field: "",
                    title: "注册来源",
                    width: 100,
                    templet: function (d) {
                        if (d.regist_type == 0) {
                            return "后台";
                        }
                        if (d.regist_type == 1) {
                            return "WEB";
                        }
                        if (d.regist_type == 2) {
                            return "iOS";
                        }
                        if (d.regist_type == 3) {
                            return "Android";
                        } else {
                            return "";
                        }
                    },
                },
                {
                    field: "",
                    title: "禁播时长",
                    width: 120,
                    templet: function (d) {
                        if (d.anchor_banned) {
                            return (
                                "<span class='layui-badge layui-bg-red'>" +
                                d.anchor_banned.ban_time_format +
                                "</span>"
                            );
                        }
                        return "";
                    },
                },
                {
                    field: "",
                    title: "分成比例",
                    width: 100,
                    templet: function (d) {
                        return d.sharing_ratio + "%";
                    },
                },
                {
                    field: "",
                    title: "所属公会",
                    width: 180,
                    templet: function (d) {
                        return d.guild ? d.guild.name : "平台";
                    },
                },
                {
                    field: "",
                    title: "转化贵族总数",
                    width: 150,
                    align: "center",
                    templet: function (d) {
                        if (!d.recommend) {
                            return "0";
                        }
                        if (!d.recommend.total_noble) {
                            return "0";
                        }

                        return d.recommend.total_noble;
                    },
                },
                {
                    field: "",
                    title: "转化新用户总数",
                    width: 160,
                    align: "center",
                    templet: function (d) {
                        if (!d.recommend) {
                            return "0";
                        }
                        if (!d.recommend.total_recommend) {
                            return "0";
                        }

                        return d.recommend.total_recommend;
                    },
                },
                {
                    title: "操作",
                    width: 270,
                    align: "center",
                    fixed: "right",
                    toolbar: "#table-userlist-operation",
                },
            ],
        ],
        page: true,
        limit: 20,
        height: "full-180",
        text: "对不起，加载出现异常！",
        even: true,
    });


    exports('administrator', {})
});